<?php
/*
 * Generates Reports
 */
class Generate_Reports extends I_Private {
	public function __construct() {
		parent::__construct ();
		
		$this->load->model ( array (
				'leiloes/Leilao_Model' 
		) );
		$this->load->helper ( 'reports' );
	}
	function index() {
		show_404 ();
	}
	
	//
	function statistics_get($id, $type) {
		$type = strtolower ( $type );
		
		$header = '<b>Relatório Prestação de Contas</b>';
		
		switch ($type) :
			
			case 'xls' :
				
				$this->load->library ( 'reports/writexls' );
				$tr = $this->writexls;
				
				break;
			case 'pdf' :
				$this->load->library ( 'reports/writepdf' );
				
				$tr = $this->writepdf;
				$tr->config ( array (
						'orientation' => 'L',
						'paper' => 'A3',
						'header' => $header 
				) );
				break;
			default :
				echo Utils::message('no_report');
				return;
				break;
		endswitch
		;
		
		$tr->add_style ( 'dataodd', 'Helvetica', '9', '', '#00000', '#ffffff' );
		$tr->add_style ( 'dataeven', 'Helvetica', '9', '', '#00000', '#f0f0f0' );
		$tr->add_style ( 'title', 'Arial', '10', 'B', '#ffffff', '#777777' );
		$tr->add_style ( 'group', 'Arial', '9', 'B', '#ffffff', '#aaaaaa' );
		$tr->add_style ( 'header', 'Arial', '12', 'B', '#ffffff', '#aaaaaa' );
		$tr->add_style ( 'header2', 'Arial', '11', '', '#ffffff', '#aaaaaa' );
		$tr->add_style ( 'final', 'Arial', '9', 'BI', '#ffffff', '#6666666' );
		$tr->add_style ( 'end_doc', 'Arial', '9', '', '#555555', '#FFFFFF' );
		$tr->add_style ( 'purple', 'Helvetica', '9', '', '#FFFFFF', '#0470B6' );
		
		$d = $this->Leilao_Model->get_info_close ( $id );
		
		$tr->add_widths ( array (
				15,
				125,
				40,
				30,
				30,
				25,
				30,
				25,
				25,
				16,
				15,
				15,
				15 
		) );
		
		$tr->start_header ();
		$tr->add_row ();
		$tr->add_cell ( utf8_decode ( 'RELATÓRIO DETALHADO DO LEILÃO - ' . $this->mask_num_leilao($d ['0']->ID) ), 'C', 'header', 13 );
		$tr->add_row ();
		$tr->add_cell ( 'COMITENTE - ' . utf8_decode ( $d ['0']->RAZAO ) . ' | HORA - ' . auto_format_data ( $d ['0']->DATA, '-', '/' ), 'L', 'header2', 13, 'LR' );
		
		$tr->add_row ();
		$tr->add_cell ( utf8_decode ( 'LOTE' ), 'C', 'title' );
		$tr->add_cell ( utf8_decode ( 'DESCRIÇÃO' ), 'L', 'title' );
		$tr->add_cell ( utf8_decode ( 'CATEGORIA' ), 'C', 'title' );
		$tr->add_cell ( utf8_decode ( 'LC. INICIAL' ), 'L', 'title' );
		$tr->add_cell ( utf8_decode ( 'VMV' ), 'L', 'title' );
		$tr->add_cell ( utf8_decode ( 'GANHADOR' ), 'L', 'title' );
		$tr->add_cell ( utf8_decode ( 'LC. GANHADOR' ), 'L', 'title' );
		$tr->add_cell ( utf8_decode ( 'COMISSÃO' ), 'L', 'title' );
		$tr->add_cell ( utf8_decode ( 'LOCAL' ), 'C', 'title' );
		$tr->add_cell ( utf8_decode ( 'VISITAS' ), 'L', 'title' );
		$tr->add_cell ( utf8_decode ( 'USERS' ), 'C', 'title' );
		$tr->add_cell ( utf8_decode ( 'BIDS' ), 'C', 'title' );
		$tr->add_cell ( utf8_decode ( 'COND.' ), 'C', 'title' );
		$tr->end_header ();
		
		$clr = false;
		$lances_ini = 0;
		$lances_vmv = 0;
		$lances_ch = 0;
		$total_a_pagar = 0;
		$total_bids = 0;
		$total_visitas = 0;
		$total_lotes = 0;
		foreach ( $d as $row ) :
			// for($i = 0; $i < 40; $i += 1) {
			$style = $clr ? 'dataeven' : 'dataodd';
			$tr->add_row ();
			$tr->add_cell ( $row->NUM_LOTE, 'C', $style );
			$tr->add_cell ( utf8_decode ( $row->LOTE_DESCRICAO ), 'L', $style );
			$tr->add_cell ( utf8_decode ( $row->CATEGORIA ), 'C', $style );
			$tr->add_cell ( get_money_format ( $row->LANCE_INICIAL, 'BRL' ), 'L', $style );
			$tr->add_cell ( get_money_format ( $row->VALOR_MINIMO, 'BRL' ), 'L', $style );
			$tr->add_cell ( utf8_decode ( $row->LOGIN ), 'L', $style );
			$tr->add_cell ( get_money_format ( $row->LANCE_GANHADOR, 'BRL' ), 'L', $style );
			$a_pagar = ($row->LANCE_GANHADOR * $row->COMISSAO) / 100;
			$tr->add_cell ( get_money_format ( $a_pagar, 'BRL' ), 'L', $style );
			$tr->add_cell ( utf8_decode ( $row->CIDADE ) . '/' . utf8_decode ( $row->UF_LOTE ), 'C', $style );
			$tr->add_cell ( $row->VISITAS, 'C', 'group' );
			
			$tr->add_cell ( ( int ) $row->USERS, 'C', 'group' );
			$tr->add_cell ( ( int ) $row->BIDS, 'C', 'group' );
			$tr->add_cell ( utf8_decode ( $row->VALOR_MINIMO > $row->LANCE_GANHADOR && $row->LOGIN != null ? 'SIM' : ($row->LOGIN != null ? 'NÃO' : '-') ), 'C', 'purple' );
			$lances_ini += $row->LANCE_INICIAL;
			$lances_vmv += $row->VALOR_MINIMO;
			$lances_ch += $row->LANCE_GANHADOR;
			$total_a_pagar += $a_pagar;
			$total_bids += ( int ) $row->BIDS;
			$total_visitas += $row->VISITAS;
			$total_lotes += 1;
			$clr = ! $clr;
			
			// }
		endforeach
		;
		
		$tr->add_row ();
		$tr->add_cell ( $total_lotes, 'C', 'title' );
		$tr->add_cell ( '', 'L', 'title' );
		$tr->add_cell ( '', 'C', 'title' );
		$tr->add_cell ( get_money_format ( $lances_ini, 'BRL' ), 'L', 'title' );
		$tr->add_cell ( get_money_format ( $lances_vmv, 'BRL' ), 'L', 'title' );
		$tr->add_cell ( '', 'L', 'title' );
		$tr->add_cell ( get_money_format ( $lances_ch, 'BRL' ), 'L', 'title' );
		$tr->add_cell ( get_money_format ( $total_a_pagar, 'BRL' ), 'L', 'title' );
		$tr->add_cell ( '', 'C', 'title' );
		$tr->add_cell ( $total_visitas, 'c', 'title' );
		$tr->add_cell ( '', 'C', 'title' );
		$tr->add_cell ( $total_bids, 'C', 'title' );
		$tr->add_cell ( '', 'C', 'title' );
		// $tr->_render();
		// $tr->show_file ();
		$tr->download_file ( 'detalhamento-' . time () );
	}
	
	/*
	 *  generateReports
	 *  
	 *  geração dos devidos relatórios configurados.
	 *  
	 * 
	 */
	function generateReports($id, $name, $show ,$type ) {
		$type = strtolower ( $type );
		
		
		
		switch ($type) :
			
			case 'xls' :
				
				$this->load->library ( 'reports/writexls' );
				$tr = $this->writexls;
				
				break;
			case 'pdf' :
				$this->load->library ( 'reports/writepdf' );
				
				$tr = $this->writepdf;
				$tr->config ( array (
						'orientation' => 'P',
						'paper' => 'A4',
				) );
				break;
			default :
				echo Utils::message('no_report');
				return;
				break;
		endswitch;
		
		
		if(!method_exists($this,$name.'_get')){
			header('content-type:text/json');
			echo json_encode(array('report'=>Utils::message('no_report')));
			return;
		}
	    call_user_func_array(array($this,$name.'_get'),array($tr,$id,$show));
		
		
	
	}
	
	//function reports
	
	private function sumary_detail_get($obj,$id,$show){
		
		$data = $this->Leilao_Model->get_statistics_close($id);
		//adicionando styles
		$obj->add_style ( 'dataodd', 'Helvetica', '9', '', '#00000', '#ffffff' );
		$obj->add_style ( 'dataeven', 'Helvetica', '9', '', '#00000', '#f0f0f0' );
		$obj->add_style ( 'title', 'Arial', '10', 'B', '#ffffff', '#777777' );
		$obj->add_style ( 'group', 'Arial', '9', '', '#333333', '#CCCCCC' );
		$obj->add_style ( 'header', 'Arial', '12', 'B', '#ffffff', '#aaaaaa' );
		$obj->add_style ( 'header2', 'Arial', '11', '', '#ffffff', '#aaaaaa' );
		$obj->add_style ( 'final', 'Arial', '9', 'BI', '#ffffff', '#6666666' );
		$obj->add_style ( 'end_doc', 'Arial', '9', '', '#555555', '#FFFFFF' );
		$obj->add_style ( 'blue_header', 'Helvetica', '12', 'B', '#FFFFFF', '#0657A2' );
		$obj->add_style ( 'clear', 'Helvetica', '9','', '#FFFFFF', '#ffffff' );
		
		
		$WD = array(17,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10);
		$obj->add_widths($WD);
		
		if(count($data) == 0):
		$obj->add_row ();
		$obj->add_cell ( utf8_decode ( Utils::message('no_data') ), 'C', 'header', count($WD) );
		else:
		
		$obj->start_header();
		$obj->add_row();
		$obj->add_cell(utf8_decode('RELATÓRIOS ESTATÍSTICOS DETALHADO'),'C','blue_header',count($WD),0);
		$obj->end_header();
		
		$obj->add_row();
		$obj->add_cell('','C','dataodd',1,0);
		$obj->add_row();
		$obj->add_cell(utf8_decode('LEILÃO Nº '. $this->mask_num_leilao($data->leilao->ID) . ' : '.$data->leilao->COMITENTE),'C','title',count($WD),1);
		$obj->add_row();
		$obj->add_cell(' ','C','title',4);
		$obj->add_cell('QTD','C','title',1);
		$obj->add_cell('LANCE INICIAL','C','title',3);
		$obj->add_cell('VMV','C','title',3);
		$obj->add_cell('LANCE RESERVADO','C','title',4);
		$obj->add_cell(utf8_decode('ÁGIO RESERVADO'),'C','title',4);
		
		
		$obj->add_row();
		$obj->add_cell(utf8_decode('NUM. LOTES COM LANCE'),'L','group',4,'BLR');
		$obj->add_cell($data->lotes_com_lances->NUM != null ? $data->lotes_com_lances->NUM : 0,'C','dataeven',1,'BRL');
		$obj->add_cell(get_money_format ( $data->lotes_com_lances->LANCE_INICIAL, 'BRL' ),'C','dataeven',3,'BRL');
		$obj->add_cell(get_money_format ( $data->lotes_com_lances->VMV, 'BRL' ),'C','dataeven',3,'BRL');
		$obj->add_cell(get_money_format ( $data->lotes_com_lances->LANCE, 'BRL' ),'C','dataeven',4,'BRL');
		$obj->add_cell(0.00,'C','dataeven',4,'BRL');
		
		$obj->add_row();
		$obj->add_cell(utf8_decode('NUM. LOTES VENDIDOS'),'L','group',4,'BLR');
		$obj->add_cell($data->lotes_vendidos->NUM,'C','dataodd',1,'BRL');
		$obj->add_cell(get_money_format ( $data->lotes_vendidos->LANCE_INICIAL, 'BRL' ),'C','dataodd',3,'BRL');
		$obj->add_cell(get_money_format ( $data->lotes_vendidos->VMV, 'BRL' ),'C','dataodd',3,'BRL');
		$obj->add_cell(get_money_format ( $data->lotes_vendidos->LANCE, 'BRL' ),'C','dataodd',4,'BRL');
		$obj->add_cell(0.00,'C','dataodd',4,'BRL');
		
		$obj->add_row();
		$obj->add_cell(utf8_decode('NUM. LOTES CONDICIONAIS'),'L','group',4,'BLR');
		$obj->add_cell($data->lotes_condicionais->NUM ? $data->lotes_condicionais->NUM : 0 ,'C','dataeven',1,'BRL');
		$obj->add_cell(get_money_format ( $data->lotes_condicionais->LANCE_INICIAL, 'BRL' ),'C','dataeven',3,'BRL');
		$obj->add_cell(get_money_format ( $data->lotes_condicionais->VMV, 'BRL' ),'C','dataeven',3,'BRL');
		$obj->add_cell(get_money_format ( $data->lotes_condicionais->LANCE, 'BRL' ),'C','dataeven',4,'BRL');
		$obj->add_cell(1,'C','dataeven',4,'BRL');
		
		$obj->add_row();
		$obj->add_cell(utf8_decode('NUM. LOTES SEM LANCE'),'L','group',4,'LR');
		$obj->add_cell($data->lotes_sem_lances->NUM,'C','dataodd',1,'BRL');
		$obj->add_cell(get_money_format ( $data->lotes_sem_lances->LANCE_INICIAL, 'BRL' ),'C','dataodd',3,'BRL');
		$obj->add_cell(get_money_format ( $data->lotes_sem_lances->VMV, 'BRL' ),'C','dataodd',3,'BRL');
		$obj->add_cell(0.00,'C','dataodd',4,'BRL');
		$obj->add_cell(0.00,'C','dataodd',4,'BRL');
		
		
		//SHOW ESTATISTICAS
		//
		$obj->add_row();
		$obj->add_cell('','C','dataodd',4,'T');
		$obj->add_row();
		$obj->add_cell(utf8_decode('ESTATÍSTICAS '),'L','title',count($WD),'LRTB');
		
		
		$obj->add_row();
		$obj->add_cell(utf8_decode('TOTAL HABILITADOS'),'L','group',4,'BLR');
		$obj->add_cell($data->leilao->HABILITADOS,'C','dataeven',1,'BRL');
		$obj->add_cell('','C','clear',1,'RL');
		$obj->add_cell(utf8_decode('COMPRADORES WEB X PREGÃO'),'C','group',6,'BRL');
		
		$obj->add_row();
		$obj->add_cell(utf8_decode('TOTAL BIDS'),'L','group',4,'BLR');
		$obj->add_cell($data->leilao->BIDS,'C','dataodd',1,'BRL');
		$obj->add_cell('','C','clear',1,'RL');
		$obj->add_cell(utf8_decode('PREGÃO'),'C','group',3,'BRL');
		$obj->add_cell(utf8_decode('WEB'),'C','group',3,'BRL');
		//$obj->add_cell(utf8_decode('DTQ'),'C','group',1,'BRL');
		
		
		
		
		$obj->add_row();
		$obj->add_cell(utf8_decode('USUÁRIOS COM LANCE'),'L','group',4,'BLR');
		$obj->add_cell($data->leilao->USERS_BIDS,'C','dataeven',1,'BRL');
		$obj->add_cell('','C','clear',1,'RL');
		$obj->add_cell(0,'C','dataeven',3,'BRL');
		$obj->add_cell($data->leilao->TOTAL_LOTES,'C','dataeven',3,'BRL');
		//$obj->add_cell(2,'C','dataeven',1,'BRL');
		
		
		
		$obj->add_row();
		$obj->add_cell('','L','clear',5,'T');
		$obj->add_cell('','C','clear',1,'R');
	
		$obj->add_cell('0%','C','dataodd',3,'BRL');
		$obj->add_cell('100%','C','dataodd',3,'BRL');
		
		$t_array = array(
					
				'estados'=>array(
						array('uf'=>'sp','bids'=>12,'cadastros'=>1),
						array('uf'=>'ce','bids'=>39,'cadastros'=>3)
				)
		);
		$obj->add_row();
		$obj->add_cell('','C','dataodd',1,0);
		$obj->add_row();
		$obj->add_cell(utf8_decode('PROCEDÊNCIA POR ESTADO '),'L','title',5,'LRTB');
		$obj->add_cell('','C','clear',1,'LR	');
		$obj->add_cell(utf8_decode('CADASTROS POR ESTADO'),'L','title',6,'LRTB');
		
		$obj->add_row();
		$obj->add_cell(utf8_decode('ESTADO '),'C','group',2,'LRTB');
		$obj->add_cell(utf8_decode('BIDS'),'C','group',3,'LRTB');
		$obj->add_cell('','C','clear',1,'LR	');
		$obj->add_cell(utf8_decode('ESTADO'),'C','group',3,'LRTB');
		$obj->add_cell(utf8_decode('CADASTROS'),'C','group',3,'LRTB');
		$_data = array();
		foreach ($data->estados as $row){
			if(!isset($_data[$row->ESTADO])){
				$_data[$row->ESTADO] = array('uf'=>$row->ESTADO,'bids'=>0,'cadastros'=>array());
			}
			 
			 
			$_data[$row->ESTADO]['bids'] +=1;
			 
			if(!in_array($row->USER_ID,$_data[$row->ESTADO]['cadastros'])){
				$_data[$row->ESTADO]['cadastros'][] = $row->USER_ID;
			}
			 
		}
		
		
		
		$crl = false;
		foreach ($_data as $row){
			$style = $crl  ? 'dataodd' : 'dataeven';
				
			$obj->add_row();
			$obj->add_cell(utf8_decode($row['uf']),'C',$style,2,'LRTB');
			$obj->add_cell(utf8_decode($row['bids']),'C',$style,3,'LRTB');
			$obj->add_cell('','C','clear',1,'LR	');
			$obj->add_cell(utf8_decode($row['uf']),'C',$style,3,'LRTB');
			$obj->add_cell(count($row['cadastros']),'C',$style,3,'LRTB');
				
			$crl = !$crl;
		}
		
		$obj->add_row();
		$obj->add_cell('','C','clear',1,0);
		$obj->add_row();
		$obj->add_cell(utf8_decode('TOTAL ESTADOS - '. count($_data)),'L','title',count($WD),'LRTB');
		
		endif;
		if($show== 'yes'){
		  $obj->show_file ();
		  return;
		}
		$obj->download_file ( 'detalhamento-' . time () );
		
	}
	
	
	/*
	 * 
	 */
	
	private  function sellers_get($obj,$id,$show){
		
		$d = $this->Leilao_Model->get_lotes_vendidos($id);
		
		$obj->config(array('paper'=>'A3','orientation'=>'L'));
		
		

		$obj->add_style ( 'dataodd', 'Helvetica', '9', '', '#00000', '#ffffff' );
		$obj->add_style ( 'dataeven', 'Helvetica', '9', '', '#00000', '#f0f0f0' );
		$obj->add_style ( 'title', 'Arial', '10', 'B', '#ffffff', '#777777' );
		$obj->add_style ( 'group', 'Arial', '9', 'B', '#ffffff', '#aaaaaa' );
		$obj->add_style ( 'header', 'Arial', '12', 'B', '#ffffff', '#aaaaaa' );
		$obj->add_style ( 'header2', 'Arial', '11', '', '#ffffff', '#aaaaaa' );
		$obj->add_style ( 'final', 'Arial', '9', 'BI', '#ffffff', '#6666666' );
		$obj->add_style ( 'end_doc', 'Arial', '9', '', '#555555', '#FFFFFF' );
		$obj->add_style ( 'purple', 'Helvetica', '9', '', '#FFFFFF', '#0470B6' );
		
		
		$columns = array (
				15,
				125,
				40,
				30,
				30,
				25,
				30,
				25,
				25,
				16,
				15,
				15,
				15
		);
		$obj->add_widths ($columns  );
		if(count($d) == 0):
		$obj->add_row ();
		$obj->add_cell ( utf8_decode ( Utils::message('no_data') ), 'C', 'header', count($columns) );
		else:
		$obj->start_header ();
		$obj->add_row ();
		$obj->add_cell ( utf8_decode ( 'RESULTADO DOS LOTES VENDIDOS  - LEILÃO / ' . $this->mask_num_leilao($d ['0']->ID) ), 'C', 'header', 13 );
		$obj->add_row ();
		$obj->add_cell ( 'COMITENTE - ' . utf8_decode ( $d ['0']->RAZAO ) . ' | HORA - ' . auto_format_data ( $d ['0']->DATA, '-', '/' ), 'L', 'header2', 13, 'LR' );
		
		$obj->add_row ();
		$obj->add_cell ( utf8_decode ( 'LOTE' ), 'C', 'title' );
		$obj->add_cell ( utf8_decode ( 'DESCRIÇÃO' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'CATEGORIA' ), 'C', 'title' );
		$obj->add_cell ( utf8_decode ( 'LC. INICIAL' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'VMV' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'GANHADOR' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'LC. GANHADOR' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'COMISSÃO' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'LOCAL' ), 'C', 'title' );
		$obj->add_cell ( utf8_decode ( 'VISITAS' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'USERS' ), 'C', 'title' );
		$obj->add_cell ( utf8_decode ( 'BIDS' ), 'C', 'title' );
		$obj->add_cell ( utf8_decode ( 'COND.' ), 'C', 'title' );
		$obj->end_header ();
		
		$clr = false;
		$lances_ini = 0;
		$lances_vmv = 0;
		$lances_ch = 0;
		$total_a_pagar = 0;
		$total_bids = 0;
		$total_visitas = 0;
		$total_lotes = 0;
		foreach ( $d as $row ) :
		// for($i = 0; $i < 40; $i += 1) {
		$style = $clr ? 'dataeven' : 'dataodd';
		$obj->add_row ();
		$obj->add_cell ( $row->NUM_LOTE, 'C', $style );
		$obj->add_cell ( utf8_decode ( $row->LOTE_DESCRICAO ), 'L', $style );
		$obj->add_cell ( utf8_decode ( $row->CATEGORIA ), 'C', $style );
		$obj->add_cell ( get_money_format ( $row->LANCE_INICIAL, 'BRL' ), 'L', $style );
		$obj->add_cell ( get_money_format ( $row->VALOR_MINIMO, 'BRL' ), 'L', $style );
		$obj->add_cell ( utf8_decode ( $row->LOGIN ), 'L', $style );
		$obj->add_cell ( get_money_format ( $row->LANCE_GANHADOR, 'BRL' ), 'L', $style );
		$a_pagar = ($row->LANCE_GANHADOR * $row->COMISSAO) / 100;
		$obj->add_cell ( get_money_format ( $a_pagar, 'BRL' ), 'L', $style );
		$obj->add_cell ( utf8_decode ( $row->CIDADE ) . '/' . utf8_decode ( $row->UF_LOTE ), 'C', $style );
		$obj->add_cell ( $row->VISITAS, 'C', 'group' );
			
		$obj->add_cell ( ( int ) $row->USERS, 'C', 'group' );
		$obj->add_cell ( ( int ) $row->BIDS, 'C', 'group' );
		$obj->add_cell ( utf8_decode ( $row->VALOR_MINIMO > $row->LANCE_GANHADOR && $row->LOGIN != null ? 'SIM' : ($row->LOGIN != null ? 'NÃO' : '-') ), 'C', 'purple' );
		$lances_ini += $row->LANCE_INICIAL;
		$lances_vmv += $row->VALOR_MINIMO;
		$lances_ch += $row->LANCE_GANHADOR;
		$total_a_pagar += $a_pagar;
		$total_bids += ( int ) $row->BIDS;
		$total_visitas += $row->VISITAS;
		$total_lotes += 1;
		$clr = ! $clr;
			
		// }
		endforeach
		;
		
		$obj->add_row ();
		$obj->add_cell ( $total_lotes, 'C', 'title' );
		$obj->add_cell ( '', 'L', 'title' );
		$obj->add_cell ( '', 'C', 'title' );
		$obj->add_cell ( get_money_format ( $lances_ini, 'BRL' ), 'L', 'title' );
		$obj->add_cell ( get_money_format ( $lances_vmv, 'BRL' ), 'L', 'title' );
		$obj->add_cell ( '', 'L', 'title' );
		$obj->add_cell ( get_money_format ( $lances_ch, 'BRL' ), 'L', 'title' );
		$obj->add_cell ( get_money_format ( $total_a_pagar, 'BRL' ), 'L', 'title' );
		$obj->add_cell ( '', 'C', 'title' );
		$obj->add_cell ( $total_visitas, 'c', 'title' );
		$obj->add_cell ( '', 'C', 'title' );
		$obj->add_cell ( $total_bids, 'C', 'title' );
		$obj->add_cell ( '', 'C', 'title' );
		// $obj->_render();
		endif;
		if($show== 'yes'){
		  $obj->show_file ();
		  return;
		}
		$obj->download_file ( 'detalhamento-' . time () );
		
	}
	
	private  function not_sellers_get($obj,$id,$show){
	
		$d = $this->Leilao_Model->get_lotes_nao_vendidos($id);
	
		$obj->config(array('paper'=>'A3','orientation'=>'L'));
	
	
	
		$obj->add_style ( 'dataodd', 'Helvetica', '9', '', '#00000', '#ffffff' );
		$obj->add_style ( 'dataeven', 'Helvetica', '9', '', '#00000', '#f0f0f0' );
		$obj->add_style ( 'title', 'Arial', '10', 'B', '#ffffff', '#777777' );
		$obj->add_style ( 'group', 'Arial', '9', 'B', '#ffffff', '#aaaaaa' );
		$obj->add_style ( 'header', 'Arial', '12', 'B', '#ffffff', '#aaaaaa' );
		$obj->add_style ( 'header2', 'Arial', '11', '', '#ffffff', '#aaaaaa' );
		$obj->add_style ( 'final', 'Arial', '9', 'BI', '#ffffff', '#6666666' );
		$obj->add_style ( 'end_doc', 'Arial', '9', '', '#555555', '#FFFFFF' );
		$obj->add_style ( 'purple', 'Helvetica', '9', '', '#FFFFFF', '#0470B6' );
	
	
	    $columns =  array (
				15,
				165,
				40,
				30,
				30,
				25,
				30,
				25,
				25,
				16
		);
		$obj->add_widths ($columns);
	    
		if(count($d) == 0):
		$obj->add_row ();
		$obj->add_cell ( utf8_decode ( Utils::message('no_data') ), 'C', 'header', count($columns) );
		else:
		
		
		$obj->start_header ();
		$obj->add_row ();
		$obj->add_cell ( utf8_decode ( 'RESULTADO DOS LOTES SEM VENDIDOS  - LEILÃO / ' . $this->mask_num_leilao($d ['0']->ID) ), 'C', 'header', count($columns));
		$obj->add_row ();
		$obj->add_cell ( 'COMITENTE - ' . utf8_decode ( $d ['0']->RAZAO ) . ' | HORA - ' . auto_format_data ( $d ['0']->DATA, '-', '/' ), 'L', 'header2', count($columns), 'LR' );
	
		$obj->add_row ();
		$obj->add_cell ( utf8_decode ( 'LOTE' ), 'C', 'title' );
		$obj->add_cell ( utf8_decode ( 'DESCRIÇÃO' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'CATEGORIA' ), 'C', 'title' );
		$obj->add_cell ( utf8_decode ( 'LC. INICIAL' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'VMV' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'GANHADOR' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'LC. GANHADOR' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'COMISSÃO' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'LOCAL' ), 'C', 'title' );
		$obj->add_cell ( utf8_decode ( 'VISITAS' ), 'L', 'title' );
		//$obj->add_cell ( utf8_decode ( 'USERS' ), 'C', 'title' );
		//$obj->add_cell ( utf8_decode ( 'BIDS' ), 'C', 'title' );
		
		$obj->end_header ();
	
		$clr = false;
		$lances_ini = 0;
		$lances_vmv = 0;
		$lances_ch = 0;
		$total_a_pagar = 0;
		$total_bids = 0;
		$total_visitas = 0;
		$total_lotes = 0;
		foreach ( $d as $row ) :
		// for($i = 0; $i < 40; $i += 1) {
		$style = $clr ? 'dataeven' : 'dataodd';
		$obj->add_row ();
		$obj->add_cell ( $row->NUM_LOTE, 'C', $style );
		$obj->add_cell ( utf8_decode ( $row->LOTE_DESCRICAO ), 'L', $style );
		$obj->add_cell ( utf8_decode ( $row->CATEGORIA ), 'C', $style );
		$obj->add_cell ( get_money_format ( $row->LANCE_INICIAL, 'BRL' ), 'L', $style );
		$obj->add_cell ( get_money_format ( $row->VALOR_MINIMO, 'BRL' ), 'L', $style );
		$obj->add_cell ( utf8_decode ( $row->LOGIN ), 'L', $style );
		$obj->add_cell ( get_money_format ( $row->LANCE_GANHADOR, 'BRL' ), 'L', $style );
		$a_pagar = ($row->LANCE_GANHADOR * $row->COMISSAO) / 100;
		$obj->add_cell ( get_money_format ( $a_pagar, 'BRL' ), 'L', $style );
		$obj->add_cell ( utf8_decode ( $row->CIDADE ) . '/' . utf8_decode ( $row->UF_LOTE ), 'C', $style );
		$obj->add_cell ( $row->VISITAS, 'C', 'group' );
			
		//$obj->add_cell ( ( int ) $row->USERS, 'C', 'group' );
		//$obj->add_cell ( ( int ) $row->BIDS, 'C', 'group' );
		$lances_ini += $row->LANCE_INICIAL;
		$lances_vmv += $row->VALOR_MINIMO;
		$lances_ch += $row->LANCE_GANHADOR;
		$total_a_pagar += $a_pagar;
		$total_bids += ( int ) $row->BIDS;
		$total_visitas += $row->VISITAS;
		$total_lotes += 1;
		$clr = ! $clr;
			
		// }
		endforeach
		;
	
		$obj->add_row ();
		$obj->add_cell ( $total_lotes, 'C', 'title' );
		$obj->add_cell ( '', 'L', 'title' );
		$obj->add_cell ( '', 'C', 'title' );
		$obj->add_cell ( get_money_format ( $lances_ini, 'BRL' ), 'L', 'title' );
		$obj->add_cell ( get_money_format ( $lances_vmv, 'BRL' ), 'L', 'title' );
		$obj->add_cell ( '', 'L', 'title' );
		$obj->add_cell ( get_money_format ( $lances_ch, 'BRL' ), 'L', 'title' );
		$obj->add_cell ( get_money_format ( $total_a_pagar, 'BRL' ), 'L', 'title' );
		$obj->add_cell ( '', 'C', 'title' );
		$obj->add_cell ( $total_visitas, 'c', 'title' );
		//$obj->add_cell ( '', 'C', 'title' );
		//$obj->add_cell ( $total_bids, 'C', 'title' );
		endif;
		// $obj->_render();
		if($show== 'yes'){
		  $obj->show_file ();
		  return;
		}
		$obj->download_file ( 'detalhamento-' . time () );
	
	}
	/* 
	 * conditional_get
	 * Recupera e gera um relatório com lotes condicionais do leilão
	 * @param object $obj objeto escritor e gerador de relatórios
	 * @param int $id id leilão 
	 * 
	 */
	private  function conditional_get($obj,$id,$show){
	
		$d = $this->Leilao_Model->get_lotes_conditionals($id);
	
		$obj->config(array('paper'=>'A3','orientation'=>'L'));
	
	
	
		$obj->add_style ( 'dataodd', 'Helvetica', '9', '', '#00000', '#ffffff' );
		$obj->add_style ( 'dataeven', 'Helvetica', '9', '', '#00000', '#f0f0f0' );
		$obj->add_style ( 'title', 'Arial', '10', 'B', '#ffffff', '#777777' );
		$obj->add_style ( 'group', 'Arial', '9', 'B', '#ffffff', '#aaaaaa' );
		$obj->add_style ( 'header', 'Arial', '12', 'B', '#ffffff', '#aaaaaa' );
		$obj->add_style ( 'header2', 'Arial', '11', '', '#ffffff', '#aaaaaa' );
		$obj->add_style ( 'final', 'Arial', '9', 'BI', '#ffffff', '#6666666' );
		$obj->add_style ( 'end_doc', 'Arial', '9', '', '#555555', '#FFFFFF' );
		$obj->add_style ( 'purple', 'Helvetica', '9', '', '#FFFFFF', '#0470B6' );
	
	    $columns =  array (
				15,
				125,
				40,
				30,
				30,
				25,
				30,
				25,
				25,
				16,
				15,
				15,
				15
		) ;
	
		$obj->add_widths ($columns);
		
		if(count($d) == 0):
		$obj->add_row ();
		$obj->add_cell ( utf8_decode ( Utils::message('no_data') ), 'C', 'header', count($columns) );
		
		endif;
		
		if(count($d) > 0):
		$obj->start_header ();
		$obj->add_row ();
		$obj->add_cell ( utf8_decode ( 'RESULTADO DOS LOTES CONDICIONAIS  - LEILÃO / ' . $this->mask_num_leilao($d ['0']->ID) ), 'C', 'header', 13 );
		$obj->add_row ();
		$obj->add_cell ( 'COMITENTE - ' . utf8_decode ( $d ['0']->RAZAO ) . ' | HORA - ' . auto_format_data ( $d ['0']->DATA, '-', '/' ), 'L', 'header2', 13, 'LR' );
	
		$obj->add_row ();
		$obj->add_cell ( utf8_decode ( 'LOTE' ), 'C', 'title' );
		$obj->add_cell ( utf8_decode ( 'DESCRIÇÃO' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'CATEGORIA' ), 'C', 'title' );
		$obj->add_cell ( utf8_decode ( 'LC. INICIAL' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'VMV' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'GANHADOR' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'LC. GANHADOR' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'COMISSÃO' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'LOCAL' ), 'C', 'title' );
		$obj->add_cell ( utf8_decode ( 'VISITAS' ), 'L', 'title' );
		$obj->add_cell ( utf8_decode ( 'USERS' ), 'C', 'title' );
		$obj->add_cell ( utf8_decode ( 'BIDS' ), 'C', 'title' );
		$obj->add_cell ( utf8_decode ( 'COND.' ), 'C', 'title' );
		$obj->end_header ();
	
		$clr = false;
		$lances_ini = 0;
		$lances_vmv = 0;
		$lances_ch = 0;
		$total_a_pagar = 0;
		$total_bids = 0;
		$total_visitas = 0;
		$total_lotes = 0;
		
		foreach ( $d as $row ) :
		// for($i = 0; $i < 40; $i += 1) {
		$style = $clr ? 'dataeven' : 'dataodd';
		$obj->add_row ();
		$obj->add_cell ( $row->NUM_LOTE, 'C', $style );
		$obj->add_cell ( utf8_decode ( $row->LOTE_DESCRICAO ), 'L', $style );
		$obj->add_cell ( utf8_decode ( $row->CATEGORIA ), 'C', $style );
		$obj->add_cell ( get_money_format ( $row->LANCE_INICIAL, 'BRL' ), 'L', $style );
		$obj->add_cell ( get_money_format ( $row->VALOR_MINIMO, 'BRL' ), 'L', $style );
		$obj->add_cell ( utf8_decode ( $row->LOGIN ), 'L', $style );
		$obj->add_cell ( get_money_format ( $row->LANCE_GANHADOR, 'BRL' ), 'L', $style );
		$a_pagar = ($row->LANCE_GANHADOR * $row->COMISSAO) / 100;
		$obj->add_cell ( get_money_format ( $a_pagar, 'BRL' ), 'L', $style );
		$obj->add_cell ( utf8_decode ( $row->CIDADE ) . '/' . utf8_decode ( $row->UF_LOTE ), 'C', $style );
		$obj->add_cell ( $row->VISITAS, 'C', 'group' );
			
		$obj->add_cell ( ( int ) $row->USERS, 'C', 'group' );
		$obj->add_cell ( ( int ) $row->BIDS, 'C', 'group' );
		$obj->add_cell ( utf8_decode ( $row->VALOR_MINIMO > $row->LANCE_GANHADOR && $row->LOGIN != null ? 'SIM' : ($row->LOGIN != null ? 'NÃO' : '-') ), 'C', 'purple' );
		$lances_ini += $row->LANCE_INICIAL;
		$lances_vmv += $row->VALOR_MINIMO;
		$lances_ch += $row->LANCE_GANHADOR;
		$total_a_pagar += $a_pagar;
		$total_bids += ( int ) $row->BIDS;
		$total_visitas += $row->VISITAS;
		$total_lotes += 1;
		$clr = ! $clr;
			
		// }
		endforeach
		;
	
		$obj->add_row ();
		$obj->add_cell ( $total_lotes, 'C', 'title' );
		$obj->add_cell ( '', 'L', 'title' );
		$obj->add_cell ( '', 'C', 'title' );
		$obj->add_cell ( get_money_format ( $lances_ini, 'BRL' ), 'L', 'title' );
		$obj->add_cell ( get_money_format ( $lances_vmv, 'BRL' ), 'L', 'title' );
		$obj->add_cell ( '', 'L', 'title' );
		$obj->add_cell ( get_money_format ( $lances_ch, 'BRL' ), 'L', 'title' );
		$obj->add_cell ( get_money_format ( $total_a_pagar, 'BRL' ), 'L', 'title' );
		$obj->add_cell ( '', 'C', 'title' );
		$obj->add_cell ( $total_visitas, 'c', 'title' );
		$obj->add_cell ( '', 'C', 'title' );
		$obj->add_cell ( $total_bids, 'C', 'title' );
		$obj->add_cell ( '', 'C', 'title' );
		endif;
		// $obj->_render();
		if($show== 'yes'){
		  $obj->show_file ();
		  return;
		}
		$obj->download_file ( 'detalhamento-' . time () );
	
	}
	private function mask_num_leilao($id){
		return str_pad ($id, 9, '0', STR_PAD_LEFT );
	}
}